package com.kaijie.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class RedisUtils {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ValueOperations<String, String> valueOperations;
    @Autowired
    private HashOperations<String, String, Object> hashOperations;
    @Autowired
    private ListOperations<String, Object> listOperations;
    @Autowired
    private SetOperations<String, Object> setOperations;
    @Autowired
    private ZSetOperations<String, Object> zSetOperations;
//    @Autowired
//    private BaseOrderService baseOrderService;
    /**
     * 默认过期时长，单位：秒
     */
    public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
    /**
     * 不设置过期时长
     */
    public final static long NOT_EXPIRE = -1;

    public void set(String key, Object value, long expire) {
        valueOperations.set(key, toJson(value));
        if (expire != NOT_EXPIRE) {
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
    }

    public void set(String key, Object value) {
        set(key, value, DEFAULT_EXPIRE);
    }

    public <T> T get(String key, Class<T> clazz, long expire) {
        String value = valueOperations.get(key);
        if (expire != NOT_EXPIRE) {
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value == null ? null : fromJson(value, clazz);
    }

    public <T> T get(String key, Class<T> clazz) {
        return get(key, clazz, NOT_EXPIRE);
    }

    public String get(String key, long expire) {
        String value = valueOperations.get(key);
        if (expire != NOT_EXPIRE) {
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value;
    }

    public String get(String key) {
        return get(key, NOT_EXPIRE);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    /**
     * 发布消息
     */
    public void publish(String channel, Object msg) {
        stringRedisTemplate.convertAndSend(channel, msg);
    }

    /**
     * Object转成JSON数据
     */
    private String toJson(Object object) {
        if (object instanceof Integer || object instanceof Long || object instanceof Float ||
                object instanceof Double || object instanceof Boolean) {
            return String.valueOf(object);
        }
        return JSON.toJSONString(object);
    }

    /**
     * JSON数据，转成Object
     */
    private <T> T fromJson(String json, Class<T> clazz) {
        return JSON.parseObject(json, clazz);
    }


    public void hSet(String h, String key, Object val) {
        hashOperations.put(h, key, toJson(val));
    }

    public <T> T hGet(String h, String key, Class<T> clazz) {
        Object o = hashOperations.get(h, key);
        try {
            return o == null ? null : fromJson(o.toString(), clazz);
        } catch (JSONException e) {
            hRemove(h, key);
            //log.error("Redis hash get exception", e);
            return null;
        }
    }

    public Long hRemove(String h, Object... key) {
        return hashOperations.delete(h, key);
    }


    public Date zSetScore(String key, Object value) {
        Double score = zSetOperations.score(key, value);
        if (score == null) return null;
        return new Date(score.longValue());
    }

    public void zSetAdd(String key, Object value, double minutes) {
        zSetOperations.add(key, value, minutes);
    }

    public void zSetRemove(String key, Object value) {
        zSetOperations.remove(key, value);
    }

    public Set<Object> zSetRangeByScore(String key, Long min, Long max) {
       return zSetOperations.rangeByScore(key, min, max);
    }

}
